package com.itheima.ydd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.entity.Link;
import com.itheima.ydd.service.LinkService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Create by Boller on 2022/2/27 20:43
 * 友情链接管理
 */

@SuppressWarnings("All")
@RestController
@RequestMapping("/ydd_link")
public class LinkController {
    @Autowired
    private LinkService linkService;

    /**
     * 友情链接--分页查询
     *
     * @param page
     * @param limit
     * @param type
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Integer type,Integer page, Integer limit) {
        Page<Link> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<Link> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(type!=null,Link::getType,type);
        linkService.page(pageInfo, queryWrapper);

        return ResultData.ok(PageResult.init(pageInfo));
    }

    /**
     * 友情链接管理--添加
     *
     * @param link
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Link link) {
        linkService.save(link);
        return ResultData.ok("添加成功");
    }

    /**
     * 友情链接管理--根据id查询单个数据
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Integer id) {
        return ResultData.ok(linkService.getById(id));
    }

    /**
     * 友情链接管理--根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(Integer id) {

        linkService.removeById(id);
        return ResultData.ok("删除成功");
    }

    /**
     * 友情链接管理--修改
     *
     * @param link
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Link link) {

        linkService.updateById(link);
        return ResultData.ok("修改成功");
    }
}
